package com.boyu.common;

import com.boyu.common.model.Menu;
import com.boyu.common.showmodel.ShowMenu;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2016/8/30.
 */
public class MenuLogic {
	private static MenuLogic instance = null;

	private MenuLogic() {
	}

	public static MenuLogic getInstance() {
		if (instance == null) {
			instance = new MenuLogic();
		}
		return instance;
	}



	public List<ShowMenu> menuLoad() {
		List<Menu> allMenuList = Menu.dao.find("select * from t_menu");
		List<ShowMenu> menuList = new ArrayList<>();
		List<Menu> parentList = allMenuList.stream().filter(menu -> menu.getParentId() == 0).collect(Collectors.toList());
		for (Menu menu : parentList) {
			ShowMenu parentShowMenu = new ShowMenu(menu);
			menuList.add(parentShowMenu);
			addChildNode(allMenuList,parentShowMenu);
		}
		return menuList;
	}

	public void addChildNode(List<Menu> list, ShowMenu parentShowMenu) {
		if (list == null) {
			return;
		}
		List<Menu> parentList = list.stream().filter(menu -> menu.getParentId() == parentShowMenu.getMenu().getId()).collect(Collectors.toList());
		for (Menu column : parentList) {
			ShowMenu subMenu = new ShowMenu(column);
			subMenu.setParentMenu(parentShowMenu.getMenu());
			parentShowMenu.addSubShowMenu(subMenu);
			addChildNode(list,subMenu);
		}
	}
}
